home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hsend.exe / HSEND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-11  |  24.9 KB  |  1,066 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <dir.h>
  5. #include <dos.h>
  6. #include <time.h>
  7. #include <async.h>
  8. #include <process.h>
  9. #include "hsend.h"
  10.  
  11. int WAITING,BAUD,COMM,RINGS,SCREENFLAG,LOGFILEFLAG,FLAGFILEFLAG;
  12. int HARDERR_FLAG=0,TIMEOUT=180;
  13. char LOGFILE[61],FLAGFILE[61],PASSWORD[13];
  14. ASYNC *P=NULL;
  15.  
  16.  
  17. main(argc,argv)
  18. int argc;
  19. char argv[];
  20. {
  21. ctrlbrk(break_handler);
  22. harderr(hardware_error_handler);
  23. load_data();
  24.  
  25. if(argc>2){
  26.     cprintf("\n\rUsage:  hsend <no arguments>    Run in host mode\n\r");
  27.     cprintf("        hsend <batch file name> Run in guest mode\n\r");
  28.     beep(3,400);
  29.     exit(1);}
  30.  
  31. copyright();
  32.  
  33. if(!init_modem()){
  34.     gotoxy(1,21);
  35.     textcolor(LIGHTGRAY);
  36.     cprintf("\n\rCOMM device did not respond.  Check settings with CONFIGUR.\n\r");
  37.     beep(3,400);
  38.     exit(1);}
  39.  
  40. clrscr();
  41. textcolor(LIGHTCYAN);
  42. empty_box(1,1,80,20);
  43. empty_box(1,21,80,24);
  44.  
  45. if(argc==2) guest_mode(argv[2]);
  46.  else host_mode();
  47.  
  48. window(1,1,80,25);
  49. gotoxy(3,22);
  50. textcolor(LIGHTMAGENTA);
  51. cprintf("Initializing modem, please wait...");
  52. init_modem();
  53. a_close(P,0);
  54.  
  55. textcolor(LIGHTGRAY);
  56. textbackground(BLACK);
  57. clrscr();
  58. exit(0);
  59. }
  60.  
  61.  
  62. void host_mode(void)
  63. {
  64. int x,y;
  65. char temp[80];
  66. struct tm *ptr;
  67. time_t lt;
  68.  
  69. TIMEOUT*=2;                                    /* wait longer if in host mode */
  70. gotoxy(29,1);
  71. cprintf(" H-SEND Host Mode V1.0 ");
  72. window(4,2,77,18);
  73.  
  74. for(;;){
  75.  bottom(&x,&y);
  76.  cprintf("Modem is currently OFF-LINE.  Press ESC to exit.");
  77.  a_printf(P,"AT S0=%d V0\n\r",RINGS);
  78.  do{
  79.     if(kbhit()) if(getch()==27){                    /* ESC key to exit       */
  80.         clrscr();
  81.         return;}
  82.     }while(OFFLINE);                            /* wait here for carrier */
  83.  
  84. cprintf("\n\rModem is currently ON-LINE");
  85. lt=time(NULL);
  86. ptr=localtime(<);
  87. strcpy(temp,"On-line: ");
  88. strcat(temp,asctime(ptr));
  89. strcat(temp,"\n\r");
  90. report(temp);
  91. top(x,y);
  92. if(password_check()) while(cmd_mode(1));
  93. init_modem();
  94. init_modem();                                    /* make sure modem is reset */
  95. }
  96. }
  97.  
  98.  
  99. int guest_mode(batchfile)
  100. char *batchfile;
  101. {
  102. FILE *fp;
  103. char str[100],temp[80],*buf;
  104. int i,x,y,flag=1;
  105. struct tm *ptr;
  106. time_t lt;
  107.  
  108. if(FLAGFILEFLAG) remove(FLAGFILE);
  109.  
  110. strcpy(str,"Executing: ");
  111. strcat(str,batchfile);
  112. gotoxy(39-strlen(batchfile),1);
  113. cprintf(" %s ",str);
  114. window(4,3,77,18);
  115.  
  116. AGAIN:
  117. if((fp=fopen(batchfile,"r"))==NULL){
  118.     report("ERROR- Unable to open batchfile");
  119.     return 0;}                                /* return error condition */
  120. do{
  121.     if(fgets(str,100,fp)){
  122.         for(i=0;i<strlen(str);i++)
  123.             if(str[i]=='\n' || str[i]=='\r') str[i]=NULL;
  124.     strupr(str);
  125.     textcolor(WHITE);
  126.     report("");
  127.     report(str);
  128.     textcolor(LIGHTCYAN);
  129.     flag=interpret_batch_cmd(str);}
  130. }while(!feof(fp) && !flag && !HARDERR_FLAG);
  131.  
  132. scb(EOC,0,NULL);
  133. lt=time(NULL);
  134. ptr=localtime(<);
  135. strcpy(temp,"Call terminated: ");
  136. strcat(temp,asctime(ptr));
  137. report(temp);
  138. fclose(fp);
  139. if(flag || HARDERR_FLAG){
  140.     report("ON-LINE transfer was not sucessful");
  141.     x=wherex(); y=wherey();
  142.     if((buf=malloc(2*44*9))==NULL) exit(1);
  143.     gettext(18,5,62,13,buf);
  144.     window(18,5,62,13);
  145.     textbackground(LIGHTCYAN);
  146.     textcolor(WHITE);
  147.     clrscr();
  148.     empty_box(2,1,44,9);
  149.     gotoxy(11,1);
  150.     cprintf(" Data Transfer Failure ");
  151.     gotoxy(5,4);
  152.     cprintf("The transfer has not been completed.");
  153.         beep(1,300);
  154.     beep(1,1000);
  155.     do{
  156.         gotoxy(5,6);
  157.         cprintf("Would you like to try again? <Y or N>");
  158.         i=getch();
  159.         i=toupper(i);
  160.         if(i!='Y' && i!='N') beep(1,1000);
  161.         }while(i!='Y' && i!='N');
  162.     cprintf("%c",i);
  163.     textbackground(BLACK);
  164.     window(4,3,77,18);
  165.     puttext(18,5,62,13,buf);
  166.     gotoxy(x,y);
  167.     if(i=='Y') goto AGAIN;
  168.  
  169.     window(4,3,77,18);
  170.     puttext(18,5,62,13,buf);
  171.     return 0;}
  172.  
  173.  else{
  174.     report("No error occurred");
  175.     if(FLAGFILEFLAG) if((fp=fopen(FLAGFILE,"a"))==NULL){
  176.         if((fp=fopen(FLAGFILE,"r"))==NULL) report("Unable to create flagfile");}
  177.     return 1;}
  178.  
  179. }
  180.  
  181.  
  182. int cmd_mode(host_flag)                            /* return 0 at end of sesssion      */
  183. {
  184. unsigned char buf[518];
  185. int cmdreq;
  186.  
  187. for(;;){
  188.  if(host_flag){
  189.     if(!scb(OKTS,0,NULL)){                        /* OK to send                       */
  190.         report("ERROR- OKTS transmission not acknowledged");
  191.         return 0;}}
  192.  
  193.  
  194.  do{                                            /* receiving command                */
  195.     if(OFFLINE){
  196.         report("DATA CONNECTION LOST");
  197.         return 0;}
  198.     cmdreq=rcb(0,buf);
  199.     }while(!cmdreq);
  200.  
  201. switch(cmdreq){
  202.  
  203.     case OKTS: return -1;
  204.  
  205.     case SFR: send_file_request(buf,0);
  206.             break;
  207.  
  208.     case IFR: create_local_file(buf);
  209.             break;
  210.  
  211.     case DF:  delete_files(buf,HOST);
  212.             break;
  213.  
  214.     case EM:  error_message(buf);
  215.             break;
  216.  
  217.     case EOC: report("Transmission terminated normally");
  218.             return 0;
  219.  
  220.     case RESTART: report("Restarting hsend...");
  221.                 execlp("hsend.exe",NULL,NULL);
  222.  
  223.     default:  report("cmd_mode() default");
  224.             if(!host_flag) return -1;}            /* switch back to active mode */
  225.  
  226.  
  227. } /* endless loop */
  228. }
  229.  
  230.  
  231. int passive(void)                                /* switch to passive mode */
  232. {
  233. int status;
  234.  
  235. for(;;){
  236.     status=cmd_mode(0);                            /* will be executing received commands while here */
  237.  
  238.     if(!status) return 1;                        /* outgoing command not acknowledged, returning 1 will
  239.                                                cause guest_mode() to exit with an error condition  */
  240.  
  241.     if(status==-1) return 0;                        /* OKTS received, switch back to send mode        */
  242.  
  243. } /* endless loop */
  244. }
  245.  
  246.  
  247. int interpret_batch_cmd(cmd)
  248. char *cmd;
  249. {
  250. char temp[512],from[100],to[100];
  251. int i;
  252.  
  253. if(strlen(cmd)<4) return 0;                        /* ignore trash */
  254.  
  255. if(!strncmp(cmd,"CALL",4)) return(call_remote(cmd));
  256.  
  257. if(!strncmp(cmd,"REM",3)) return 0;                /* remarks */
  258.  
  259. if(scb(EM,2," ")) if(!rcb(OKTS,temp)) rcb(OKTS,temp);    /* send a blank line */
  260. if(scb(EM,strlen(cmd)+1,cmd)) if(!rcb(OKTS,temp)) rcb(OKTS,temp); /* send the command  */
  261.  
  262. /*----------------------- COPY FROM HOST TO GUEST --------------------------------*/
  263. if(!strncmp(cmd,"COPY H",6)){
  264.     parse(cmd);
  265.     if(!seperate(cmd,from,to,' ')){
  266.         report("ERROR- COPY command syntax");
  267.         return 1;}
  268.     for(i=0;i<strlen(from);i++)                    /* remove H and L designations */
  269.         from[i]=from[i+1];
  270.     for(i=0;i<strlen(to);i++)
  271.         to[i]=to[i+1];
  272.  
  273.     strcpy(temp,from);                            /* set up SFR string */
  274.     strcat(temp,"|");
  275.     strcat(temp,to);
  276.  
  277.     if(!scb(SFR,strlen(temp)+1,temp)){
  278.         report("ERROR- Host unable to verify SFR transmission");
  279.         return 1;}
  280.      else return passive();}
  281. /*--------------------------------------------------------------------------------*/
  282.  
  283. /*----------------------- COPY FROM GUEST TO HOST --------------------------------*/
  284. if(!strncmp(cmd,"COPY L",6)){
  285.     parse(cmd);
  286.     if(!seperate(cmd,from,to,' ')){
  287.         report("ERROR- COPY command syntax");
  288.         return 1;}
  289.     for(i=0;i<strlen(from);i++)                    /* remove H and L designations */
  290.         from[i]=from[i+1];
  291.     for(i=0;i<strlen(to);i++)
  292.         to[i]=to[i+1];
  293.  
  294.     strcpy(temp,from);                            /* set up SFR string */
  295.     strcat(temp,"|");
  296.     strcat(temp,to);
  297.  
  298.     return send_file_request(temp,1);}
  299. /*--------------------------------------------------------------------------------*/
  300.  
  301.  
  302. /*-------------------------DELETE FILES-------------------------------------------*/
  303. if(!strncmp(cmd,"DELETE",6)){
  304.     parse(cmd);
  305.     if(cmd[0]=='H'){
  306.         for(i=0;i<strlen(cmd);i++)
  307.             cmd[i]=cmd[i+1];                    /* remove the first character (H) */
  308.         {if(!scb(DF,strlen(cmd)+1,cmd)){
  309.             report("ERROR- Host unable to verify DF transmission");
  310.             return 1;}
  311.          else return passive();}}
  312.      if(cmd[0]=='L'){
  313.           for(i=0;i<strlen(cmd);i++)
  314.             cmd[i]=cmd[i+1];                    /* remove the first character (H) */
  315.         delete_files(cmd,GUEST);
  316.         return 0;}}
  317. /*--------------------------------------------------------------------------------*/
  318.  
  319. report("ERROR- Command syntax");
  320. return 1;
  321. }
  322.  
  323.  
  324. void report(message)
  325. char *message;
  326. {
  327.  
  328. FILE *fp;
  329.  
  330. if(SCREENFLAG) cprintf("%s\n\r",message);
  331.  
  332. if(!LOGFILEFLAG) return;
  333. if((fp=fopen(LOGFILE,"a"))==NULL){
  334.     if((fp=fopen(LOGFILE,"r"))==NULL) return;}
  335.  
  336. fprintf(fp,"%s\n\r",message);
  337. fclose(fp);
  338. }
  339.  
  340.  
  341. int init_modem(void)
  342. {
  343. int x;
  344.  
  345. if(P) a_close(P,0);
  346. P=a_open(COMM,BAUD,0,8,1,8000,2000);                /* open at no parity,8 dbits, 1 sbit 8K input ,2k output buffer */
  347. a_setmcr(P,MCR_DTR+MCR_RTS);                        /* set DTR and RTS high                    */
  348. a_iflush(P);                                    /* clear input buffer                      */
  349. sleep(1);
  350. a_puts("+++",P);
  351. sleep(2);
  352. a_puts("\n\rATZ\n\r",P);
  353. sleep(2);
  354. a_puts("AT S0=0\n\r",P);
  355. a_waitquiet(P,18,72,1);                            /* wait for a reply                        */
  356. x=a_icount(P);
  357. while(a_icount(P)) a_getc(P);                        /* clear input buffer                      */
  358. return x;                                        /* return # of characters in reply         */
  359. }                                            /* 0 return value denotes no modem present */
  360.  
  361.  
  362. int agetc(void)                                /* returns 0 if error occurs     */
  363. {
  364. long timer;
  365.  
  366. timer=biostime(0,0);
  367. do{
  368.     if(OFFLINE) return -1;                        /* no carrier                    */
  369.     if(a_icount(P)) return a_getc(P);
  370.     }while(biostime(0,0)-timer<TIMEOUT);
  371.  
  372. report("Timeout");
  373. return -2;
  374. }
  375.  
  376.  
  377. int password_check(void)                            /* return 1 if password is verified */
  378. {
  379. unsigned char temp[518];
  380.  
  381. cprintf("\n\r\n\rWaiting for password...\n\r");
  382. sleep(3);
  383. if(!scb(RP,0,NULL)){
  384.     report("Caller could not verify Requesting Password transmission");
  385.     return 0;}
  386. if(!rcb(SP,temp)){
  387.     report("Caller did not sent password");
  388.     return 0;}
  389. if(!strcmp(temp,PASSWORD)) return 1;                /* correct password received */
  390. report("Incorrect password received.");
  391. return 0;
  392. }
  393.  
  394.  
  395. int password_verification(void)                    /* 0 return value equals password match */
  396. {
  397. unsigned char tmp[518];
  398.  
  399. while(a_icount(P)) a_getc(P);                        /* clear input buffer                      */
  400.                                             /* host will wait 3 seconds before sending request */
  401. if(!rcb(RP,tmp)) if(!rcb(RP,tmp)){
  402.      report("ERROR- Did not receive 'requesting password' transmission");
  403.      return 1;}
  404.  
  405. if(!scb(SP,strlen(PASSWORD)+1,PASSWORD)){
  406.     report("ERROR- Host unable to verify password transmission");
  407.     return 1;}
  408.  
  409. if(!rcb(OKTS,tmp)){
  410.     report("ERROR- did not receive OKTS after host received password transmission");
  411.     return 1;}
  412.  
  413. /* call will be dropped at host end if password string does not match */
  414.  
  415. return 0;
  416. }
  417.  
  418.  
  419. int call_remote(number)
  420. char *number;
  421. {
  422. char tmp[100];
  423. int x,y;
  424. struct tm *ptr;
  425. time_t lt;
  426.  
  427. bottom(&x,&y);
  428. cprintf("Phone call in progress...");
  429. init_modem();
  430.  
  431. parse(number);
  432. a_printf(P,"AT V0 S7=%d X4 TD%s\r",WAITING,number);     /* send modem command  */
  433. a_gets(tmp,100,P,36);                            /* receive echo of command */
  434. a_gets(tmp,100,P,WAITING*18);
  435.  
  436. top(x,y);
  437.  
  438. lt=time(NULL);
  439. ptr=localtime(<);
  440. switch(atoi(tmp)){
  441.  
  442.     case 1: if(BAUD==300){
  443.             strcpy(tmp,"300 BPS connection: ");
  444.             strcat(tmp,asctime(ptr));
  445.             report(tmp);
  446.             return(password_verification());}
  447.             else{
  448.                 report("ERROR- Unable to connect a proper baud rate");
  449.                 return 1;}
  450.  
  451.     case 5: if(BAUD==1200){
  452.             strcpy(tmp,"1200 BPS connection: ");
  453.             strcat(tmp,asctime(ptr));
  454.             report(tmp);
  455.             return(password_verification());}
  456.             else{
  457.                 report("ERROR- Unable to connect at proper baud rate");
  458.                 return 1;}
  459.  
  460.     case 9:
  461.     case 10: if(BAUD==2400){
  462.             strcpy(tmp,"2400 BPS connection: ");
  463.             strcat(tmp,asctime(ptr));
  464.             report(tmp);
  465.             return(password_verification());}
  466.             else{
  467.                 report("ERROR- Unable to connect at proper baud rate");
  468.                 return 1;}
  469.  
  470.     case 3: report("ERROR- No carrier");
  471.            return 1;
  472.  
  473.     case 6: report("ERROR- No dialtone");
  474.            return 1;
  475.  
  476.     case 7: report("ERROR- Dialed number is busy");
  477.            return 1;
  478.  
  479.     case 8: report("ERROR- 5 seconds of silence not detected (using @ command)");
  480.            return 1;
  481.  
  482.     default: report("ERROR- Dialing error");
  483.             return 1;}
  484.  
  485. }
  486.  
  487.  
  488. int scb(cmd,blklen,blk)                            /* send command block, returns 1 if acknowledged */
  489. int cmd;                                        /* #defined command code            */
  490. int blklen;                                    /* number of data characters        */
  491. unsigned char *blk;                                /* 0 to 512 data characters         */
  492. {                                            /* returns value of carrier detect  */
  493. unsigned char buf[518];
  494. register int i,try,reply;
  495. unsigned long cksum;
  496.  
  497. while(a_icount(P)) a_getc(P);                        /* clear input buffer     */
  498. buf[3]=cmd;                                    /* command code           */
  499. buf[4]=blklen/255;                                /* # of data chars hibyte */
  500. buf[5]=blklen-(buf[4]*255);                        /* # of data chars lobyte */
  501.  
  502. if(blklen){                                    /* if there are data characters */
  503.     for(i=0;i<blklen;i++)                        /* copy them into buf           */
  504.         buf[i+6]=blk[i];}
  505.  
  506. for(i=3,cksum=0;i<blklen+6;i++)                    /* add to create checksum */
  507.     cksum+=buf[i];
  508.  
  509. buf[0]=cksum/65025;                                /* checksum hibyte  */
  510. cksum-=(long)buf[0]*65025;
  511. buf[1]=cksum/255;                                /* checksum midbyte */
  512. buf[2]=cksum-(long)(buf[1]*255);                    /* checksum lobyte  */
  513.  
  514. for(try=0;try<TRY;try++){
  515.     for(i=0;i<blklen+6;i++)
  516.         a_putc(buf[i],P);                        /* send the block   */
  517.     reply=agetc();
  518.     if(reply==ACK)    return 1;                        /* OK               */
  519.     if(reply==-1) return 0;                        /* no carrier       */
  520.     if(reply!=NAK)    while(a_icount(P)) a_getc(P);}    /* remove the trash */
  521.  
  522. return 0;
  523. }
  524.  
  525.  
  526. int rcb(cmd,blk)                                /* receive command block              */
  527. int cmd;                                        /* expected reply command             */
  528. unsigned char *blk;                                /* fills blk with data chars received */
  529. {                                            /* returns 0 if cannot verify block on TRY attempts */
  530. long cksum,tempsum;
  531. unsigned char buf[518];
  532. register int i;
  533. int tmp,charcount;
  534.  
  535. for(i=0;i<6;i++){                                /* get first 6 characters         */
  536.     tmp=agetc();
  537.     if(tmp<0)    return 0;
  538.     buf[i]=tmp;}
  539.  
  540. charcount=(buf[4]*255)+buf[5];                    /* get remaining data characters  */
  541. if(charcount){
  542.     for(i=6;i<charcount+6;i++){
  543.         tmp=agetc();
  544.         if(tmp<0) return 0;                        /* no carrier or timeout          */
  545.         buf[i]=tmp;
  546.         blk[i-6]=tmp;}}
  547.  
  548. for(tempsum=0,i=3;i<charcount+6;i++)                 /* create checksum of chars received */
  549.     tempsum+=buf[i];
  550.  
  551. cksum=(long)buf[0]*65025;                        /* create cheksum sent with data     */
  552. cksum+=(long)buf[1]*255;
  553. cksum+=buf[2];
  554.  
  555. if(cmd==0) cmd=buf[3];                            /* unknown command expected          */
  556.  
  557. if(cksum==tempsum && cmd!=buf[3]) return 0;
  558.  
  559. {if(cksum==tempsum && cmd==buf[3]){
  560.     a_putc(ACK,P);
  561.     return buf[3];}                            /* block OK returning cmd received   */
  562.  else{
  563.     a_putc(NAK,P);                                /* block not OK                      */
  564.     return 0;}}
  565. }
  566.  
  567.  
  568. int rdb(cmd,blk)                                /* receive data block                 */
  569. int cmd;                                        /* expected reply command             */
  570. unsigned char *blk;                                /* fills blk with data chars received */
  571. {                                            /* returns 0 if cannot verify block on TRY attempts */
  572. long cksum,tempsum;
  573. unsigned char buf[518];
  574. register int i;
  575. int tmp,charcount;
  576.  
  577. for(i=0;i<6;i++){                                /* get first 6 characters         */
  578.     tmp=agetc();
  579.     if(tmp<0)    return -1;
  580.     buf[i]=tmp;}
  581.  
  582. charcount=(buf[4]*255)+buf[5];                    /* get remaining data characters  */
  583. if(charcount){
  584.     for(i=6;i<charcount+6;i++){
  585.         tmp=agetc();
  586.         if(tmp<0)    return -1;                    /* no carrier or timeout          */
  587.         buf[i]=tmp;
  588.         blk[i-6]=tmp;}}
  589.  
  590. for(tempsum=0,i=3;i<charcount+6;i++)                 /* create checksum of chars received */
  591.     tempsum+=buf[i];
  592.  
  593. cksum=(long)buf[0]*65025;                        /* create cheksum sent with data     */
  594. cksum+=(long)buf[1]*255;
  595. cksum+=buf[2];
  596.  
  597. if(cmd==0) cmd=buf[3];                            /* unknown command expected          */
  598.  
  599. if(cksum==tempsum && cmd!=buf[3]) return 0;
  600.  
  601. {if(cksum==tempsum && cmd==buf[3]){
  602.     a_putc(ACK,P);
  603.     return charcount;}                            /* block OK returning cmd received   */
  604.  else{
  605.     a_putc(NAK,P);                                /* block not OK                      */
  606.     return -1;}}
  607. }
  608.  
  609.  
  610. int create_local_file(filespec)
  611. char *filespec;
  612. {
  613. unsigned char buf[518];
  614. char tmp[100];
  615. FILE *fp;
  616. int charcount,i,try,x,y;
  617. long size=0;
  618.  
  619. strcpy(tmp,"Creating local file: ");
  620. strcat(tmp,filespec);
  621. report(tmp);
  622.  
  623.  
  624. if((fp=fopen(filespec,"wb"))==NULL){                /* open the file */
  625.     strcpy(tmp,"ERROR- Unable to create file");
  626.     report(tmp);
  627.     scb(EM,strlen(tmp),tmp);
  628.     return 1;}
  629.  
  630. if(!scb(IFS,0,NULL)){                            /* start sending now */
  631.     report("ERROR- Host unable to verify IFS transmission");
  632.     fclose(fp);
  633.     return 1;}
  634.  
  635. bottom(&x,&y);
  636. cprintf("File transfer in progress...\n\rBytes received: 0");
  637.  
  638. try=1;
  639. do{
  640.     charcount=rdb(SDB,buf);
  641.     if(OFFLINE){
  642.         fclose(fp);
  643.         top(x,y);
  644.         report("ERROR- file transfer aborted (connection lost)");
  645.         return 1;}                            /* no carrier */
  646.     if(++try>TRY && charcount<0){
  647.         fclose(fp);
  648.         top(x,y);
  649.         report("ERROR- file transfer aborted (receiving error)");
  650.         return 1;}
  651.     if(charcount){
  652.         try=1;
  653.         for(i=0;i<charcount;i++){
  654.             if(ferror(fp)){
  655.                 fclose(fp);
  656.                 top(x,y);
  657.                 report("ERROR- File transfer aborted (I/O error)");
  658.                 return 1;}
  659.             fputc(buf[i],fp);}}
  660. size+=charcount;
  661. gotoxy(1,2);
  662. cprintf("Bytes received: %ld",size);
  663. }while(charcount);                                /* stop when you rec. an empty block */
  664.  
  665. i=ferror(fp);
  666. fclose(fp);
  667. top(x,y);
  668. return i;                                        /* non zero equals error condition */
  669. }
  670.  
  671.  
  672. void error_message(msg)
  673. char *msg;
  674. {
  675. char temp[200];
  676. if(strlen(msg)>2) strcpy(temp,"Remote: ");
  677.  else strcpy(temp," ");
  678. strcat(temp,msg);
  679. report(temp);
  680.  
  681. }
  682.  
  683.  
  684. void delete_files(filespec,hostflag)
  685. char *filespec;
  686. int hostflag;                                    /* set to 0 if deleting a file while in guest mode */
  687. {
  688. char temp[150],path[100],pathname[116];
  689. struct ffblk f;
  690. int i;
  691.  
  692. strcpy(temp,"Deleting file(s): ");
  693. strcat(temp,filespec);
  694. report(temp);
  695.  
  696. strcpy(path,filespec);                            /* create a path for remove command */
  697. for(i=strlen(path);i>0;i--){
  698.     if(path[i]=='\\') break;
  699.     path[i]=NULL;}
  700.  
  701. if(findfirst(filespec,&f,0)){
  702.     strcpy(temp,"No file(s) to delete");
  703.     report(temp);
  704.     if(hostflag) scb(EM,strlen(temp)+1,temp);
  705.     return;}
  706.  
  707. do{
  708.  strcpy(temp,f.ff_name);                         /* temp used to report what files deleted */
  709.  
  710.  strcpy(pathname,path);                            /* pathname is file to remove */
  711.  strcat(pathname,f.ff_name);
  712.  
  713.  {if(!remove(pathname)){
  714.     for(i=0;i<14-strlen(f.ff_name);i++)
  715.         strcat(temp," ");
  716.     strcat(temp,"deleted");
  717.     report(temp);
  718.     if(hostflag) scb(EM,strlen(temp)+1,temp);}
  719.   else{
  720.     for(i=0;i<14-strlen(f.ff_name);i++)
  721.         strcat(temp," ");
  722.     strcat(temp,"cannot be deleted");
  723.     report(temp);
  724.     if(hostflag)  scb(EM,strlen(temp)+1,temp);}}
  725.  
  726. }while(!findnext(&f));
  727. }
  728.  
  729.  
  730. int send_file_request(fromto,send_OKTS_flag)            /* returns 1 if error, 0 on success */
  731. unsigned char *fromto;                            /* holds name of files to send */
  732. int send_OKTS_flag;                                     /* set to 1 if sending from guest to host */
  733. {
  734. struct ffblk f;
  735. int i,c,errflag=0;
  736. char source_pathname[200],dest_pathname[200],tmp[518],from[200],to[200];
  737. char s[15],d[15];
  738.  
  739. if(!seperate(fromto,from,to,'|')){
  740.     error_message("Source/destination seperation error");
  741.     return 1;}
  742.  
  743. if(findfirst(from,&f,0)){
  744.     report("No files to send\n");
  745.     return 0;}
  746.  
  747. do{
  748. strcpy(source_pathname,from);                        /* create a full source pathname */
  749.  
  750. for(i=strlen(source_pathname);i>0;i--){
  751.     if(source_pathname[i]=='\\') break;
  752.     source_pathname[i]=NULL;}
  753.  
  754. strcat(source_pathname,f.ff_name);
  755.  
  756. strcpy(dest_pathname,to);                        /* create a full dest. pathname */
  757. for(i=strlen(dest_pathname);i>0;i--){
  758.     if(dest_pathname[i]=='\\') break;
  759.     dest_pathname[i]=NULL;}
  760.  
  761. strcpy(s,eight_plus_three(f.ff_name,'?'));            /* store 8+3 filename in s  */
  762.  
  763. for(c=0,i=strlen(to)-1;i>0;i--){                    /* store 8+3 filename in d  */
  764.     if(to[i]=='\\') break;
  765.     tmp[c++]=to[i];}
  766. tmp[c]=NULL;
  767. strrev(tmp);
  768. strcpy(d,eight_plus_three(tmp,'@'));
  769.  
  770. for(i=0;i<8;i++){                                /* convert the * char to ? */
  771.     if(d[i]=='*'){
  772.         for(;i<8;i++)
  773.             d[i]='?';}}
  774. for(i=9;i<12;i++){
  775.     if(d[i]=='*'){
  776.         for(;i<12;i++)
  777.             d[i]='?';}}
  778.  
  779. for(i=0;i<12;i++)                                /* lay s on d  */
  780.     if(d[i]!='@') if(s[i]!='?' && d[i]=='?') d[i]=s[i];
  781.  
  782. for(i=0;i<12;i++)                                /* remove trash */
  783.     if(d[i]=='@' || d[i]=='?') d[i]=' ';
  784.  
  785. for(i=0,c=0;i<12;i++)                            /* remove spaces */
  786.     if(d[i]!=' ') tmp[c++]=d[i];
  787. tmp[c]=NULL;
  788.  
  789. strcat(dest_pathname,tmp);                        /* add to filename to path */
  790. strupr(dest_pathname);
  791.  
  792. errflag=initiate_file_send(source_pathname,dest_pathname); /* send the file */
  793. if(send_OKTS_flag) if(!rcb(OKTS,tmp)) rcb(OKTS,tmp);
  794.  
  795. }while(!findnext(&f) && !errflag);
  796. return errflag;
  797. }
  798.  
  799.  
  800. int initiate_file_send(from,to)                    /* returns 1 on error, 0 if OK */
  801. char *from,*to;
  802. {
  803. unsigned char temp[518],buf[518];
  804. long sent=0;
  805. int i,x,y;
  806. struct ffblk f;
  807. FILE *fp;
  808.  
  809. if(findfirst(from,&f,0)){
  810.     report("Error reading filesize from directory");
  811.     return 1;}
  812.  
  813. strcpy(temp,"Dest: ");
  814. strcat(temp,to);
  815. report(temp);
  816.  
  817. scb(IFR,strlen(to)+1,to);                        /* send null terminated pathname */
  818.  
  819. if(!rcb(IFS,temp)){                                /* OK to send file?              */
  820.     report("Error creating remote file");
  821.     return 1;}
  822.  
  823. if((fp=fopen(from,"rb"))==NULL){
  824.     strcpy(temp,"Unable to read file: ");
  825.     strcat(temp,from);
  826.     report(temp);
  827.     return 1;}
  828.  
  829. bottom(&x,&y);
  830. cprintf("File transfer in progress...\n\rFile size: %ld          Bytes sent: %ld",f.ff_fsize,sent);
  831. do{
  832.     for(i=0;i<512;i++){
  833.         if(feof(fp) || ferror(fp)) break;
  834.         sent++;
  835.         buf[i]=fgetc(fp);}
  836.     if(feof(fp) || ferror(fp)){
  837.         sent--;
  838.         if(i) i--;}                            /* don't send an extra character */
  839.     if(!scb(SDB,i,buf)){
  840.         strcpy(temp,"Unable to verify SDB transmission");
  841.         scb(EM,strlen(temp)+1,temp);
  842.         fclose(fp);
  843.         top(x,y);
  844.         report(temp);
  845.         return 1;}
  846.  
  847. gotoxy(1,2);
  848. cprintf("File size: %ld          Bytes sent: %ld",f.ff_fsize,sent);
  849. }while(i);
  850.  
  851. if(ferror(fp)){
  852.     strcpy(temp,"ERROR- reading file");
  853.     top(x,y);
  854.     report(temp);
  855.     scb(EM,strlen(temp)+1,temp);}
  856.  
  857. fclose(fp);
  858. top(x,y);
  859. return 0;
  860. }
  861.  
  862.  
  863. int load_data(void)
  864. {
  865. FILE *fp;
  866. if((fp=fopen(DATAFILE,"r"))==NULL){
  867.     cprintf("\n\rUnable to open configuration file %s\n\r",DATAFILE);
  868.     perror("Reason");
  869.     cprintf("\n\rRun CONFIGUR to create a new config file.\n\r");
  870.     beep(3,400);
  871.     exit(1);}
  872.  
  873.  else{
  874.     fread(&BAUD,sizeof(BAUD),1,fp);
  875.     fread(&COMM,sizeof(COMM),1,fp);
  876.     fread(&RINGS,sizeof(RINGS),1,fp);
  877.     fread(&SCREENFLAG,sizeof(SCREENFLAG),1,fp);
  878.     fread(&LOGFILEFLAG,sizeof(LOGFILEFLAG),1,fp);
  879.     fread(&LOGFILE,sizeof(LOGFILE),1,fp);
  880.     fread(&FLAGFILEFLAG,sizeof(FLAGFILEFLAG),1,fp);
  881.     fread(&FLAGFILE,sizeof(FLAGFILE),1,fp);
  882.     fread(&PASSWORD,sizeof(PASSWORD),1,fp);
  883.     fread(&WAITING,sizeof(WAITING),1,fp);
  884.     fclose(fp);}
  885.  
  886. return 1;
  887. }
  888.  
  889.  
  890. void copyright(void)
  891. {
  892. system("cls");
  893. textbackground(BLACK);
  894. textcolor(LIGHTCYAN);
  895. empty_box(20,8,65,14);
  896. gotoxy(29,10);
  897. cprintf("H-SEND File Transfer System");
  898. gotoxy(22,12);
  899. cprintf("(C) John Hutchey 1991  All Rights Reserved");
  900. }
  901.  
  902.  
  903. int break_handler(void)
  904. {
  905. window(1,1,80,25);
  906. clrscr();
  907. textcolor(LIGHTGRAY);
  908. textbackground(BLACK);
  909. printf("Control-Break interrupt.\n");
  910. return 0;
  911. }
  912.  
  913.  
  914. void empty_box(start_x,start_y,end_x,end_y)
  915. int start_x,start_y,end_x,end_y;
  916. {
  917. int x,y;
  918.  
  919.                                             /* set up corners */
  920. gotoxy(start_x,start_y);
  921. cprintf("%c",201);
  922. gotoxy(end_x,start_y);
  923. cprintf("%c",187);
  924. gotoxy(start_x,end_y);
  925. cprintf("%c",200);
  926. gotoxy(end_x,end_y);
  927. cprintf("%c",188);
  928.  
  929.                                             /* top & bottom line */
  930. for(x=start_x+1;x<end_x;x++){
  931.     gotoxy(x,start_y);
  932.     cprintf("%c",205);
  933.     gotoxy(x,end_y);
  934.     cprintf("%c",205);}
  935.  
  936.                                             /* side lines */
  937. for(y=start_y+1;y<end_y;y++){
  938.     gotoxy(start_x,y);
  939.     cprintf("%c",186);
  940.     gotoxy(end_x,y);
  941.     cprintf("%c",186);}
  942.  
  943. }
  944.  
  945.  
  946. void beep(loop,freq)
  947. int loop,freq;
  948. {
  949. int i;
  950. unsigned char p;
  951. long timer;
  952.  
  953. union{
  954.     long divisor;
  955.     unsigned char c[2];
  956.     }count;
  957.  
  958. for(i=0;i<loop;i++){
  959.     count.divisor=1193280/freq;
  960.     outportb(67,182);
  961.     outportb(66,count.c[0]);
  962.     outportb(66,count.c[1]);
  963.     p=inportb(97);
  964.     outportb(97,p|3);
  965.  
  966.     timer=biostime(0,0);
  967.     while(biostime(0,0)-timer<3);
  968.  
  969.     outportb(97,p);
  970.  
  971.     timer=biostime(0,0);
  972.     while(biostime(0,0)-timer<1);}
  973. }
  974.  
  975.  
  976. int seperate(combined,first,second,seperator)
  977. char *combined,*first,*second,seperator;
  978. {
  979. int i,c;
  980.  
  981. for(i=0;i<strlen(combined);i++)
  982.     if(combined[i]==seperator) break;
  983. if(i==strlen(combined)) return 0;                    /* no seperator present in string */
  984.  
  985. for(i=0;i<strlen(combined);i++){
  986.     if(combined[i]==seperator) break;
  987.     first[i]=combined[i];}
  988. first[i]=NULL;                                    /* first half of *combined copied to *first */
  989.  
  990. for(i++,c=0;i<=strlen(combined);i++)
  991.     second[c++]=combined[i];                        /* second half of *combined copied to *second */
  992.  
  993. return 1;                                        /* OK */
  994. }
  995.  
  996.  
  997. char *eight_plus_three(source,fill)
  998. char *source;
  999. char fill;
  1000. {
  1001. static char a[15];
  1002. char b[5];
  1003. char filler[2];
  1004.  
  1005. filler[0]=fill;
  1006. filler[1]=NULL;
  1007.  
  1008. if(!seperate(source,a,b,'.')){
  1009.     strcpy(a,source);
  1010.     b[0]=NULL;}
  1011.  
  1012. while(strlen(a)<8) strcat(a,filler);                /* pad to 8 chars */
  1013. strcat(a,".");                                    /* add a period   */
  1014. strcat(a,b);                                    /* add extension  */
  1015. while(strlen(a)<12) strcat(a,filler);                /* pad to 12 chars */
  1016. return a;                                        /* return pointer to converted string */
  1017. }
  1018.  
  1019.  
  1020. void parse(str)                                 /* remove chars up to and including first space character */
  1021. char *str;
  1022. {
  1023. char temp[120];
  1024. int i=0,n=0;
  1025.  
  1026. while(str[i]!=' ') i++;
  1027.  
  1028. for(i++;i<strlen(str);i++){
  1029.     if(n==119){
  1030.         printf("Error- command string too long!\n%s\n",str);
  1031.         break;}
  1032.     temp[n++]=str[i];}
  1033.  
  1034. temp[n]=NULL;
  1035. strcpy(str,temp);
  1036. }
  1037.  
  1038.  
  1039. void top(x,y)                                    /* Switch to top window */
  1040. int x,y;
  1041. {
  1042. clrscr();
  1043. window(4,3,77,18);
  1044. gotoxy(x,y);
  1045. textcolor(LIGHTCYAN);
  1046. }
  1047.  
  1048.  
  1049. void bottom(x,y)                                /* Switch to bottom window */
  1050. int *x,*y;
  1051. {
  1052. *x=wherex();
  1053. *y=wherey();
  1054. window(4,22,79,23);
  1055. textcolor(LIGHTMAGENTA);
  1056. clrscr();
  1057. }
  1058.  
  1059.  
  1060. hardware_error_handler(int errnum,int ax,int bp,int si)
  1061. {
  1062. report("Hardware error intercepted");
  1063. HARDERR_FLAG++;
  1064. hardretn(2);
  1065. }
  1066.